package com.mjp.learn.multi.juc;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * Semaphore信号量的概念  同一时间只能有N个线程访问 超过就等着
 */
public class Demo07 implements Runnable {
    public static final Semaphore semp = new Semaphore(5);

    @Override
    public void run() {
        try{
            semp.acquire();
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getId()+":done!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semp.release();
        }
    }

    public static void main(String[] args) {
        ExecutorService exec = Executors.newFixedThreadPool(20);
        final Demo07 demo = new Demo07();
        for(int i= 0 ; i < 20 ; i ++){
            exec.submit(demo);
        }
    }
}
